package jlb.andriva.wrappers;

import jlb.andriva.R;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Consumption {

    public String mFOperacion;
    private String mFPresentacion;
    private String mDetalle;
    private String mCouta;
    private String mImportePeso;
    private String mImporteDolar;

    public Consumption(Element root){
        Elements tds = root.select("td[align=center]");
        mFOperacion = tds.get(1).text();
        mFPresentacion = tds.get(2).text();
        mDetalle = tds.get(3).text().trim();
        mCouta = tds.get(4).text();
        mImportePeso = "$ " + tds.get(5).text();
        mImporteDolar = "u$s " + tds.get(6).text();
    }

    public LinearLayout getView(Context context){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        params.setMargins(2, 2, 2, 2);

        LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mainParams.setMargins(2, 8, 2, 8);

        LinearLayout main = new LinearLayout(context);
        main.setOrientation(LinearLayout.VERTICAL);
        main.setBackgroundResource(R.drawable.border);
        main.setLayoutParams(mainParams);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        TextView lblDetalle = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblDetalle.setGravity(Gravity.LEFT);
        lblDetalle.setEllipsize(TextUtils.TruncateAt.END);
        lblDetalle.setLayoutParams(params);
        lblDetalle.setText(mDetalle);

        main.addView(lblDetalle);

        LinearLayout.LayoutParams paramsRuler = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 2);

        View ruler = (View) inflater.inflate(R.layout.ruler, null);
        ruler.setLayoutParams(paramsRuler);

        main.addView(ruler);

        LinearLayout data = new LinearLayout(context);
        data.setOrientation(LinearLayout.HORIZONTAL);

        TextView lblCuota = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblCuota.setGravity(Gravity.LEFT);
        lblCuota.setEllipsize(TextUtils.TruncateAt.END);
        lblCuota.setLayoutParams(params);
        lblCuota.setText(mCouta);

        data.addView(lblCuota);

        LinearLayout datapay = new LinearLayout(context);
        datapay.setOrientation(LinearLayout.VERTICAL);
        datapay.setGravity(Gravity.RIGHT);
        datapay.setLayoutParams(params);

        TextView lblPriceDolar = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblPriceDolar.setEllipsize(TextUtils.TruncateAt.END);
        lblPriceDolar.setLayoutParams(params);
        lblPriceDolar.setText(mImporteDolar);
        if(mImporteDolar.equals("u$s 0,00")){
            lblPriceDolar.setTextColor(Color.GRAY);
        }

        datapay.addView(lblPriceDolar);

        TextView lblPricePeso = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblPricePeso.setEllipsize(TextUtils.TruncateAt.END);
        lblPricePeso.setLayoutParams(params);
        lblPricePeso.setText(mImportePeso);
        if(mImportePeso.equals("$ 0,00")){
            lblPricePeso.setTextColor(Color.GRAY);
        }

        datapay.addView(lblPricePeso);

        data.addView(datapay);

        main.addView(data);

        return main;
    }
}
